package com.ithzl.capacity.service.impl;

import com.ithzl.capacity.mapper.EntryFormMapper;
import com.ithzl.capacity.pojo.EntryForm;
import com.ithzl.capacity.pojo.TeamQualification;
import com.ithzl.capacity.pojo.TeamWorks;
import com.ithzl.capacity.service.EntryFormService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

@Service
public class EntryFormServiceImpl implements EntryFormService {

    @Autowired
    EntryFormMapper entryFormMapper;
    @Override
    public int save(EntryForm entryForm) {
        entryFormMapper.add(entryForm);
        // 设置团队唯一编号
        for (TeamWorks teamWorks:entryForm.getTeamWorksList()){
            if (!teamWorks.getTeamNumber().contains("G")){
                String str=entryForm.getEntryFormId()+"G"+teamWorks.getWorksName();
                teamWorks.setTeamNumber(str);
            }
        }
        // 设置教学团队资格
        entryForm.replenish();
        for (TeamQualification teamQualification:entryForm.getTeamQualificationList()){
            entryFormMapper.setTeamQualification(teamQualification);
        }

        // 设置团队作品
        for (TeamWorks teamWorks:entryForm.getTeamWorksList()){
            if (entryFormMapper.selectEntryTeam(entryForm.getEntryFormId(),teamWorks.getTeamNumber())==null){
                entryFormMapper.addEntryTeam(entryForm.getEntryFormId(),teamWorks.getTeamNumber());
                entryFormMapper.addTeamWorks(teamWorks);
            }
        }
        return entryForm.getEntryFormId();

    }

    @Override
    public void update(EntryForm entryForm) {
        // 更新报名表
        entryFormMapper.updateEntryTeam(entryForm);

        // 删除教学团队资格
        for (TeamQualification teamQualification:entryForm.getTeamQualificationList()){
            if (entryFormMapper.selectTeamQualification(teamQualification)!=0){
                entryFormMapper.deleteTeamQualification(teamQualification);
            }
            // 新增教学团队资格
            entryFormMapper.setTeamQualification(teamQualification);
        }

        // 删除团队作品
        for (TeamWorks teamWorks:entryForm.getTeamWorksList()){
            int id=entryFormMapper.selectEntryTeamGetId(entryForm.getEntryFormId(),teamWorks.getTeamNumber());
            //作品中团队的编号例属表中有该映射就不用删除
            if (id!=0){
                //删除团队作品
                entryFormMapper.deleteTeamWorks(teamWorks.getTeamNumber());
            }
            else {
                if (!teamWorks.getTeamNumber().contains("G")){
                    String str=entryForm.getEntryFormId()+'G'+teamWorks.getWorksName();
                    teamWorks.setTeamNumber(str);
                }
                entryFormMapper.addEntryTeam(entryForm.getEntryFormId(),teamWorks.getTeamNumber());

            }
            // 添加团队作品
            entryFormMapper.addTeamWorks(teamWorks);
        }

        //删除多余映射
        List<String> teamNumberList=entryFormMapper.selectTeamNumberByEntryFormId(entryForm.getEntryFormId());
        for (int i=0,len=teamNumberList.size();i<len;i++){
            String str=teamNumberList.get(i);
            for (TeamWorks teamWorks:entryForm.getTeamWorksList()){
                if (str.equals(teamWorks.getTeamNumber())){
                    teamNumberList.remove(i);
                    len--;
                    i--;
                    break;
                }
            }
        }
        for (String number:teamNumberList){
            entryFormMapper.deleteEntryTeam(number);
            entryFormMapper.deleteTeamWorks(number);
        }

    }

    @Override
    public void submit(int entryFormId) {
        entryFormMapper.submit(entryFormId);
    }

    @Override
    public EntryForm selectEntryForm(int entryFormId) {
        // 获取表单数据
        EntryForm entryForm=entryFormMapper.selectEntryForm(entryFormId);

        // 获取教学团队资格
        List<TeamQualification> teamQualificationList=entryFormMapper.getTeamQualificationList(entryFormId);

        // 获取课堂实录
        List<TeamWorks> teamWorksList=entryFormMapper.selectTeamWorksList(entryFormId);

        entryForm.setTeamQualificationList(teamQualificationList);
        entryForm.setTeamWorksList(teamWorksList);
        return entryForm;
    }

}
